﻿Imports System.Data.SqlClient
Imports System.Data
Public Class AddQuestion_mobile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = True Then
            Dim controlID As String = Page.Request.Params("__EVENTTARGET")
            Dim postbackControl As Control = Page.FindControl(controlID)
            If postbackControl IsNot Nothing Then
                If postbackControl.ID = ddl_Chapter.ID And ddl_Chapter.SelectedValue > 0 Then
                    ddl_QuestionNumber.Enabled = True
                    ddl_QuestionNumber.SelectedValue = 0
                ElseIf postbackControl.ID = ddl_Chapter.ID And ddl_Chapter.SelectedValue = 0 Then
                    ddl_QuestionNumber.Enabled = False
                    ddl_QuestionNumber.SelectedValue = 0
                End If

                If postbackControl.ID = ddl_Book.ID And ddl_Book.SelectedValue = 0 Then
                    ddl_Chapter.SelectedValue = 0
                    ddl_QuestionNumber.SelectedValue = 0
                    ddl_QuestionNumber.Enabled = False
                End If

                If postbackControl.ID = ddl_QuestionNumber.ID And ddl_QuestionNumber.SelectedValue > 0 Then
                    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
                    'Checking if someone has already posted the same question on the same book on the same chapter
                    Dim cmdIsAlreadyAsked As New SqlCommand("SELECT * FROM Questions WHERE Book_FK = @f1 AND Chapter = @f2 AND Question_Number = @f3", conn)
                    cmdIsAlreadyAsked.CommandType = CommandType.Text
                    cmdIsAlreadyAsked.Parameters.AddWithValue("@f1", ddl_Book.SelectedValue)
                    cmdIsAlreadyAsked.Parameters.AddWithValue("@f2", ddl_Chapter.SelectedValue)
                    cmdIsAlreadyAsked.Parameters.AddWithValue("@f3", ddl_QuestionNumber.SelectedValue)
                    'Getting records (only 1 line for selected question)
                    conn.Open()
                    Dim dr As SqlDataReader = cmdIsAlreadyAsked.ExecuteReader()
                    If dr.HasRows Then
                        dr.Read()
                        If Not IsDBNull(dr("Book_FK")) And Not IsDBNull(dr("Chapter")) And Not IsDBNull("Question_Number") Then
                            btn_AddQuestion.Enabled = False
                            btn_AddQuestion.Text = "Question Already Asked"
                        End If
                    Else
                        btn_AddQuestion.Enabled = True
                        btn_AddQuestion.Text = "Add Question"
                    End If
                ElseIf postbackControl.ID = ddl_QuestionNumber.ID And ddl_QuestionNumber.SelectedValue = 0 Then
                    btn_AddQuestion.Enabled = False
                    btn_AddQuestion.Text = "Select Question Number"
                End If
            End If

            If rbl_Solution.SelectedValue = 1 Then
                tb_Solution.Enabled = True
            Else
                tb_Solution.Enabled = False
            End If

        End If
    End Sub

    Protected Sub btn_AddQuestion_Click(sender As Object, e As EventArgs) Handles btn_AddQuestion.Click
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        If (ddl_Chapter.SelectedValue > 0 AndAlso ddl_QuestionNumber.SelectedValue > 0) Then
            Dim SqlString As String
            If rbl_Solution.SelectedValue = 0 Then
                SqlString = "INSERT INTO Questions(Book_FK, Chapter, Question_Number, Question_Text, Questioner) VALUES (@f1, @f2, @f3, @f4, @f5)"
            Else
                SqlString = "INSERT INTO Questions(Book_FK, Chapter, Question_Number, Question_Text, Questioner, ProvidesSolution, Solution) VALUES (@f1, @f2, @f3, @f4, @f5, @f6, @f7)"
            End If
            Dim cmd As New SqlCommand(SqlString, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", ddl_Book.SelectedValue)
            cmd.Parameters.AddWithValue("@f2", ddl_Chapter.SelectedValue)
            cmd.Parameters.AddWithValue("@f3", ddl_QuestionNumber.SelectedValue)
            cmd.Parameters.AddWithValue("@f4", tb_Question.Text)
            cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
            If rbl_Solution.SelectedValue = 1 Then
                cmd.Parameters.AddWithValue("@f6", rbl_Solution.SelectedValue)
                cmd.Parameters.AddWithValue("@f7", tb_Solution.Text)
            End If
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            Response.Redirect("Questions.aspx")
        End If
    End Sub
End Class